<?php
/*
 * @Author: your name2
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-02-20 21:14:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\admin_controller;


use Illuminate\Support\Facades\Request;
use App\Models\admin_model\Admins as AdminDb;                        // admins表

class Admins extends BaseController
{

    public function adminList(){
        if (Request::has('keyWord')) {
            $whereStr = '';
            $limitStr = '';
            if (Request::input('pageSize') && Request::input('page')) {
                $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
            }
            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (!empty($keyWord)) {
                $whereStr .= havaWhereLikeJoinEqual($keyWord);
            }
          
                $list  =   AdminDb::list($whereStr, $limitStr);
                $count =   AdminDb::count($whereStr);
                return $this->create(200, '查询成功', ['list' => $list, 'total' => $count[0]['count']]);
            

        }  else{
            return   missingParameter();
        } 
    }  
   

    //添加
    public function adminAdd()
    {
        if (Request::has('formData')) {
            $formData = Request::input('formData');
            $formData['passWord'] = md5( $formData['passWord']) ;
            if( $this->checkUserName($formData['userName'],false)){
                $res =  redis_mysql_add_return('admins', $formData);
            if ($res) {
                return $this->create(200, '添加成功', ['id' => $res]);
            } else {
                return $this->create(100, '添加失败');
            }
            }else{
                return $this->create(100, '该用户名已存在');
            }
           
        }
    }
    



    //编辑
    public function adminEdit()
    {
        if (Request::has('formData') && Request::has('id')) {
            $formData = Request::input('formData');
            $id       = Request::input('id');
            if (is_array($id)) {
                $res = redis_mysql_edit_in('admins', $id, $formData);
            } else {
                if(isset($formData['passWord'])){
                    $formData['passWord'] =  md5($formData['passWord']);
                }
                if(isset($formData['userName'])){
                    $checkUserName = $this->checkUserNameFun($formData['userName']);
                    if ($checkUserName &&  $checkUserName['id'] != $id) {
                        //查到相应id并且改id不等于自己时
                        return $this->create(100, '该用户名已存在');
                    }
                }
                $res =  redis_mysql_edit_return('admins', ['id' => $id], $formData);
            }
            if ($res) {
                return $this->create(200, '修改成功');
            } else {
                return $this->create(100, '修改失败');
            }
        }
    }


    //查单条
    public function adminSelOne()
    {
        if (Request::has('id')) {
            $res =   redis_mysql_selone_return('admins', Request::input('id'), false);
            
            return $this->create(200, '查询成功', $res);
        }
    }

    public function checkUserName($userName = '',$die = true)
    {   
       
       
        if (!$this->checkUserNameFun($userName)) {
            return  $die?$this->create(200, '该账号可用'):true;
        } else {
            return  $die?$this->create(100, '该账号已存在'):false;
        }

    }

    private function checkUserNameFun($userName){
        if(empty($userName)){
            if (Request::has('userName')) {
                $userName =  Request::input('userName');
            }
        }
       
        return AdminDb::checkUserName($userName);
    }

    public function selectList(){
        return   $this->create(200,'查询成功', ['list'=>AdminDb::selectList(),'mine'=> $this->getAdminId()]);
    }


    public function mineMsg(){
         return $this->create(200,'查询成功',$this->getAdminInfo())  ;
    }
    
}
